package com.example.finaltool.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.finaltool.annotations.Timer;
import com.example.finaltool.consts.Table;
import com.example.finaltool.mapper.SysCorpVillageMapper;
import com.example.finaltool.model.CunDepInfo;
import com.example.finaltool.model.DepInfo;
import com.example.finaltool.model.config.Data;
import com.example.finaltool.model.db.SysCorpVillage;
import com.example.finaltool.utils.PrintUtils;
import com.example.finaltool.utils.SysCorpVillageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author dengbw
 * @date 2021/8/9 3:53 下午
 */
@Service
public class SysCorpVillageService {

    AdminAreaService adminAreaService;
    SysCorpVillageMapper mapper;

    @Autowired
    public SysCorpVillageService(AdminAreaService adminAreaService, SysCorpVillageMapper mapper) {
        this.adminAreaService = adminAreaService;
        this.mapper = mapper;
    }

    @Timer(value = Table.SYS_CORP_VILLAGE)
    @Transactional(rollbackFor = Exception.class)
    public void save(Data data, DepInfo info, Map<String, String> adminAreaIds) {
        String corpId = data.getCorpId();
        QueryWrapper<SysCorpVillage> wrapper = new QueryWrapper<>();
        wrapper.eq("tenant_id", corpId);
        List<SysCorpVillage> exist = mapper.selectList(wrapper);
        List<Long> existSubIds = exist.stream().map(SysCorpVillage::getSubTenantId).collect(Collectors.toList());
        saveGuanli(info.getXiangzhen(), corpId, existSubIds);
        saveFuwu(info.getFuwu(), corpId, existSubIds);
        saveCun(info.getCunDepInfoList(), adminAreaIds, corpId, existSubIds);
    }

    private void saveGuanli(Long subId, String corpId, List<Long> existSubIds) {
        if (subId == null) {
            PrintUtils.skip("乡镇管理人员");
        } else {
            SysCorpVillage xz = SysCorpVillageUtils.newXiangzhen(subId, corpId);
            if (existSubIds.contains(xz.getSubTenantId())) {
                PrintUtils.showExist(Table.SYS_CORP_VILLAGE, "乡镇管理人员");
            } else {
                mapper.insert(xz);
            }
        }
    }

    private void saveFuwu(Long subId, String corpId, List<Long> existSubIds) {
        if (subId == null) {
            PrintUtils.skip("服务支撑人员");
        } else {
            SysCorpVillage fw = SysCorpVillageUtils.newFuwu(subId, corpId);
            if (existSubIds.contains(fw.getSubTenantId())) {
                PrintUtils.showExist(Table.SYS_CORP_VILLAGE, "服务支撑人员");
            } else {
                mapper.insert(fw);
            }
        }
    }

    private void saveCun(List<CunDepInfo> cunList, Map<String, String> adminAreaIds, String corpId, List<Long> existSubIds) {
        SysCorpVillageUtils.newCunList(cunList, corpId, adminAreaIds).stream().parallel().forEach(c -> {
            if (existSubIds.contains(c.getSubTenantId())) {
                PrintUtils.showExist(Table.SYS_CORP_VILLAGE, c.getName());
            } else {
                mapper.insert(c);
            }
        });
    }
}
